

package forminrecipe.core;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import forminrecipe.data.*;

public class FileIO {
	private String sqlUserName;
	private String sqlPassword;
	private String sqlAddress;
	private String sqlDbName;
	private SQLSettings sqlSettings;
	
	public FileIO(){
		
	}
	
	public SQLSettings 	ReadSettingsFile(){
        try {
        	Properties properties = new Properties();
            FileInputStream fis = new FileInputStream("settings.xml");          
            properties.loadFromXML(fis);

            sqlAddress = properties.getProperty(constants.SETTINGS_FILE_SQLADDRESS);
            sqlUserName = properties.getProperty(constants.SETTINGS_FILE_SQLUSERNAME);
            sqlPassword = properties.getProperty(constants.SETTINGS_FILE_SQLPASSWORD);
            sqlDbName = properties.getProperty(constants.SETTINGS_FILE_SQLDBNAME);
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

		sqlSettings = new SQLSettings(sqlAddress,sqlUserName,sqlPassword, sqlDbName);
		
		return sqlSettings;
	}


	public static void saveSQLSettings(SQLSettings sqlSettings) {
		Properties properties = new Properties();
        properties.setProperty(constants.SETTINGS_FILE_SQLADDRESS, sqlSettings.sqlAddress);
        properties.setProperty(constants.SETTINGS_FILE_SQLUSERNAME, sqlSettings.sqlUserName);
        properties.setProperty(constants.SETTINGS_FILE_SQLPASSWORD, sqlSettings.sqlPassword);
        properties.setProperty(constants.SETTINGS_FILE_SQLDBNAME, sqlSettings.sqlDbName);

		try{
			FileOutputStream fos = new FileOutputStream("settings.xml");
			properties.storeToXML(fos, "Database Configuration", "UTF-8");
			fos.close();
		}catch (IOException e){
			System.out.println(e.getMessage());
		}
	}
}
